<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper" style='margin-bottom: 10px'>
      <a-form layout="inline">
        <a-row :gutter="12">
          <a-col :md="4" :sm="8">
            <a-form-item label="项目名称">
              <a-input placeholder="请输入项目名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="类别">
              <j-dict-select-tag  v-model="queryParam.projectType" placeholder="请选择项目规格"
                                  dictCode="project_type"/>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="12">
            <a-form-item label="项目负责人">
              <a-input placeholder="请输入项目负责人" v-model="queryParam.projectManager"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button
                type="primary"
                @click="searchReset"
                icon="reload"
                style="margin-left: 8px"
              >重置</a-button>

            </span>
          </a-col>

        </a-row>
        <a-row>
          <a-button type="primary" @click="handleAdd" style="margin-right: 8px" >新增</a-button>
          <a-button title="删除多条数据" @click="batchDel" type="primary">批量删除</a-button>
        </a-row>
      </a-form>
    </div>
      <a-table
        ref="table"
        bordered
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical"/>

        </span>
      </a-table>
    <project-modal ref="modalForm" @ok="modalFormOk"></project-modal>
  </a-card>
</template>

<script>
import ProjectModal from './modules/ProjectModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { deleteAction, getAction, downFile, postAction} from '@/api/manage'
import { filterObj } from '@/utils/util'
export default {
  name: 'ProjectList',
  mixins: [JeecgListMixin],
  components: {
      ProjectModal,
    // parkInfoList
  },
  data() {

    return {
      dataSource:[],
      description: '项目管理',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: "",
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
          {
              title: '项目名称',
              align: "center",
              dataIndex: 'name',
          },
          {
              title: '所属分部',
              align: "center",
              dataIndex: 'sysAreaName',
          },
        {
          title: '项目负责人 ',
          align: "center",
          dataIndex: 'projectManager',
        },
          {
              title: '地址',
              align: "center",
              dataIndex: 'address',
          },
          {
              title: '类别',
              align: "center",
              dataIndex: 'projectType_dictText',
          },
          {
              title: '规模',
              align: "center",
              dataIndex: 'projectScale_dictText',
          },
          {
              title: '电话',
              align: "center",
              dataIndex: 'phoneNumber',
          },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          customRender: x => ('1' == x ? '在用' : '停用'),
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/project/getProjectList',
        delete: '/project/delete',
        deleteBatch: '/project/deleteBatch',
        exportXlsUrl: '/project/exportXls',
        importExcelUrl: '/project/importExcel'
      },
      queryParam: {
        "name": "",
        "number":"",
        "manager":""
      },
      isorter: {
        column: 'createTime',
        order: 'desc'
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.initDictConfig();
    this.keyupEnter()
  },
  methods: {

      handleEdit: function (record) {
          this.$refs.modalForm.edit(record);
          this.$refs.modalForm.title = "编辑";
          this.$refs.modalForm.disableSubmit = false;
      },
      handleAdd: function () {
          this.$refs.modalForm.add();
          this.$refs.modalForm.title = "新增";
          this.$refs.modalForm.disableSubmit = false;
      },

      typeChange(value) {
          this.queryParam.schoolType = `${value}`
      },
    handleExportXls(fileName) {

      let param = { ...this.queryParam }

      downFile(this.url.exportXlsUrl, param).then(data => {
        if (!data) {
          this.$message.error('文件下载失败')
          return
        }
        if(data.size===0){
          this.$message.warning("未查询到学校信息")
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data]), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          window.URL.revokeObjectURL(url)
        }
      })
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.dataSource=[]
      var params = this.getQueryParams()
      this.loading = true
      postAction(this.url.list, params).then(res => {
        if (res.success&&res.result!=null) {
          this.dataSource = res.result.records

          this.ipagination.total = res.result.total

        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },

    searchQuery() {
      this.loadData(1)
    },

    getQueryParams() {
      var param = Object.assign({}, this.isorter, this.queryParam)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      return filterObj(param)
    },

    //批量删除
    batchDel: function() {
      if (!this.url.deleteBatch) {
        this.$message.error('请设置url.deleteBatch属性!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ""
        for (var a = 0; a < this.selectionRows.length; a++) {
          ids += this.selectionRows[a].id + ','
        }
        var that = this
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          onOk: function() {
            deleteAction(that.url.deleteBatch, { ids: ids }).then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData(1)
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          }
        })
      }
    },
    handleDelete: function(id) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete, { id: id }).then(res => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    handleDetail: function(record) {
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.edit(record)
    },

    keyupEnter() {
      document.onkeydown = e => {
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13) {
          this.loadData(1)
        }
      }
    },

  }
}
</script>
<style scoped>
</style>
